{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#-*-coding:utf-8 -*-\n",
    "#所有需要的库文件添加该处\n",
    "import pandas as pd\n",
    "from six import StringIO\n",
    "import matplotlib.pyplot as plt\n",
    "import  numpy as np\n",
    "import os\n",
    "from datetime import datetime,timedelta\n",
    "from pyecharts import Line,Kline,Bar,Overlap,Grid\n",
    "from jqdatasdk import *\n",
    "import requests\n",
    "import tushare as ts\n",
    "pd.set_option('precision',3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "文件所在目录：index_dir = \"C:\\\\quanttime\\\\data\\\\index\\\\\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#所用文件目录如下：\n",
    "stock_basic_info_dir = \"C:\\\\quanttime\\\\data\\\\basic_info\\\\all_stock_info.csv\"\n",
    "index_basic_info_dir = \"C:\\\\quanttime\\\\data\\\\basic_info\\\\index_all_info_for_analyse.csv\"\n",
    "index_valuation_dir = \"C:\\\\quanttime\\\\data\\\\index\\\\index_valuation\\\\\"\n",
    "stock_valuation_dir = \"C:\\\\quanttime\\\\data\\\\finance\\\\valuation\\\\\"\n",
    "index_k_dir = \"C:\\\\quanttime\\\\data\\\\index\\\\jq\\\\\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "#需要分析的指数添加到该list中\n",
    "analyse_index_list = [\"000001.XSHG\", \"000300.XSHG\", \"000905.XSHG\", \"399001.XSHE\",\\\n",
    "                      \"399005.XSHE\", \"399006.XSHE\", \"399016.XSHE\",\"399300.XSHE\",\\\n",
    "                      \"399975.XSHE\",\"399368.XSHE\",\"399959.XSHE\",\"399967.XSHE\",\"399241.XSHE\",\\\n",
    "                      \"000015.XSHG\",\"000016.XSHG\",\"000813.XSHG\",\"000814.XSHG\",\"000815.XSHG\",\\\n",
    "                     \"000005.XSHG\",\"000006.XSHG\",\"000007.XSHG\",\"000015.XSHG\",\"000816.XSHG\",\\\n",
    "                     \"000948.XSHG\",\"000043.XSHG\"]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "#加载index 基本信息\n",
    "df_index_info = pd.read_csv(index_basic_info_dir,index_col=[\"code\"],encoding=\"gbk\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "对指数的估值进行分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>名称</th>\n",
       "      <th>PB</th>\n",
       "      <th>5%分位</th>\n",
       "      <th>PB最小</th>\n",
       "      <th>PB最大</th>\n",
       "      <th>PB中位数</th>\n",
       "      <th>PB标准差</th>\n",
       "      <th>PE</th>\n",
       "      <th>5%分位</th>\n",
       "      <th>PE最小</th>\n",
       "      <th>PE最大</th>\n",
       "      <th>PE中位数</th>\n",
       "      <th>PE标准差</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>code</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>000001.XSHG</th>\n",
       "      <td>上证指数</td>\n",
       "      <td>1.53</td>\n",
       "      <td>1.310</td>\n",
       "      <td>1.21('2014/5/7')</td>\n",
       "      <td>7.14('2007/10/15')</td>\n",
       "      <td>1.90</td>\n",
       "      <td>1.097</td>\n",
       "      <td>14.82</td>\n",
       "      <td>10.420</td>\n",
       "      <td>9.04('2014/4/28')</td>\n",
       "      <td>72.54('2007/10/15')</td>\n",
       "      <td>17.480</td>\n",
       "      <td>11.439</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000300.XSHG</th>\n",
       "      <td>沪深300</td>\n",
       "      <td>1.54</td>\n",
       "      <td>1.290</td>\n",
       "      <td>1.17('2014-05-07')</td>\n",
       "      <td>7.46('2007-10-16')</td>\n",
       "      <td>1.77</td>\n",
       "      <td>1.211</td>\n",
       "      <td>13.76</td>\n",
       "      <td>9.320</td>\n",
       "      <td>8.18('2014-05-19')</td>\n",
       "      <td>65.63('2007-10-31')</td>\n",
       "      <td>14.140</td>\n",
       "      <td>10.291</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000905.XSHG</th>\n",
       "      <td>中证500</td>\n",
       "      <td>1.99</td>\n",
       "      <td>1.790</td>\n",
       "      <td>1.44('2008-11-04')</td>\n",
       "      <td>5.89('2015-06-15')</td>\n",
       "      <td>2.78</td>\n",
       "      <td>0.902</td>\n",
       "      <td>22.90</td>\n",
       "      <td>21.400</td>\n",
       "      <td>17.54('2019-01-03')</td>\n",
       "      <td>103.75('2008-01-15')</td>\n",
       "      <td>38.405</td>\n",
       "      <td>19.320</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>399001.XSHE</th>\n",
       "      <td>深证成指</td>\n",
       "      <td>2.64</td>\n",
       "      <td>1.850</td>\n",
       "      <td>1.67('2005-11-15')</td>\n",
       "      <td>8.82('2007-10-16')</td>\n",
       "      <td>2.81</td>\n",
       "      <td>1.258</td>\n",
       "      <td>23.61</td>\n",
       "      <td>12.457</td>\n",
       "      <td>10.61('2005-07-11')</td>\n",
       "      <td>63.44('2007-10-16')</td>\n",
       "      <td>21.750</td>\n",
       "      <td>11.092</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>399005.XSHE</th>\n",
       "      <td>中小板指</td>\n",
       "      <td>3.40</td>\n",
       "      <td>2.982</td>\n",
       "      <td>2.39('2008-11-04')</td>\n",
       "      <td>8.17('2007-10-10')</td>\n",
       "      <td>4.18</td>\n",
       "      <td>1.164</td>\n",
       "      <td>27.57</td>\n",
       "      <td>23.061</td>\n",
       "      <td>19.44('2008-10-27')</td>\n",
       "      <td>80.41('2008-01-15')</td>\n",
       "      <td>34.735</td>\n",
       "      <td>11.823</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>399006.XSHE</th>\n",
       "      <td>创业板指</td>\n",
       "      <td>4.73</td>\n",
       "      <td>3.054</td>\n",
       "      <td>2.51('2012-12-03')</td>\n",
       "      <td>15.01('2015-06-03')</td>\n",
       "      <td>4.73</td>\n",
       "      <td>1.937</td>\n",
       "      <td>41.95</td>\n",
       "      <td>32.755</td>\n",
       "      <td>27.99('2012-12-03')</td>\n",
       "      <td>139.96('2015-06-03')</td>\n",
       "      <td>55.045</td>\n",
       "      <td>19.503</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>399016.XSHE</th>\n",
       "      <td>深证创新指数</td>\n",
       "      <td>3.59</td>\n",
       "      <td>2.775</td>\n",
       "      <td>2.57('2019-01-03')</td>\n",
       "      <td>4.79('2017-11-13')</td>\n",
       "      <td>4.13</td>\n",
       "      <td>0.590</td>\n",
       "      <td>25.89</td>\n",
       "      <td>19.595</td>\n",
       "      <td>18.33('2019-01-03')</td>\n",
       "      <td>40.29('2017-11-13')</td>\n",
       "      <td>33.130</td>\n",
       "      <td>6.332</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>399300.XSHE</th>\n",
       "      <td>沪深300</td>\n",
       "      <td>1.54</td>\n",
       "      <td>1.290</td>\n",
       "      <td>1.17('2014-05-07')</td>\n",
       "      <td>7.46('2007-10-16')</td>\n",
       "      <td>1.78</td>\n",
       "      <td>1.211</td>\n",
       "      <td>13.76</td>\n",
       "      <td>9.320</td>\n",
       "      <td>8.18('2014-05-19')</td>\n",
       "      <td>65.63('2007-10-31')</td>\n",
       "      <td>14.150</td>\n",
       "      <td>10.290</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>399975.XSHE</th>\n",
       "      <td>中证全指证券公司指数(四级行业)</td>\n",
       "      <td>2.09</td>\n",
       "      <td>1.340</td>\n",
       "      <td>1.1('2018/10/18')</td>\n",
       "      <td>6.11('2015/6/8')</td>\n",
       "      <td>2.43</td>\n",
       "      <td>0.990</td>\n",
       "      <td>79.42</td>\n",
       "      <td>15.498</td>\n",
       "      <td>12.4('2015/9/15')</td>\n",
       "      <td>82.1('2013/9/12')</td>\n",
       "      <td>37.750</td>\n",
       "      <td>14.696</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>399368.XSHE</th>\n",
       "      <td>国证军工</td>\n",
       "      <td>2.76</td>\n",
       "      <td>2.780</td>\n",
       "      <td>2.22('2012/12/3')</td>\n",
       "      <td>11.33('2015/6/2')</td>\n",
       "      <td>4.21</td>\n",
       "      <td>1.517</td>\n",
       "      <td>69.35</td>\n",
       "      <td>70.493</td>\n",
       "      <td>59.6('2018/10/29')</td>\n",
       "      <td>326.97('2018/1/10')</td>\n",
       "      <td>133.360</td>\n",
       "      <td>49.503</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>399959.XSHE</th>\n",
       "      <td>军工指数</td>\n",
       "      <td>2.59</td>\n",
       "      <td>2.883</td>\n",
       "      <td>2.42('2012/12/3')</td>\n",
       "      <td>13.06('2015/6/2')</td>\n",
       "      <td>4.15</td>\n",
       "      <td>1.925</td>\n",
       "      <td>73.90</td>\n",
       "      <td>81.645</td>\n",
       "      <td>65.21('2018/10/19')</td>\n",
       "      <td>333.38('2015/6/12')</td>\n",
       "      <td>145.805</td>\n",
       "      <td>54.677</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>399967.XSHE</th>\n",
       "      <td>中证军工</td>\n",
       "      <td>2.96</td>\n",
       "      <td>3.303</td>\n",
       "      <td>2.89('2018/10/30')</td>\n",
       "      <td>13.37('2015/6/2')</td>\n",
       "      <td>5.90</td>\n",
       "      <td>1.976</td>\n",
       "      <td>52.84</td>\n",
       "      <td>59.727</td>\n",
       "      <td>49.07('2018/10/19')</td>\n",
       "      <td>979.83('2015/11/26')</td>\n",
       "      <td>151.990</td>\n",
       "      <td>183.414</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>399241.XSHE</th>\n",
       "      <td>地产指数</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.321</td>\n",
       "      <td>1.6('2018/10/18')</td>\n",
       "      <td>171.13('2014/12/5')</td>\n",
       "      <td>4.25</td>\n",
       "      <td>25.461</td>\n",
       "      <td>NaN</td>\n",
       "      <td>33.898</td>\n",
       "      <td>16.98('2018/9/28')</td>\n",
       "      <td>464.15('2015/11/23')</td>\n",
       "      <td>83.015</td>\n",
       "      <td>69.211</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000015.XSHG</th>\n",
       "      <td>红利指数</td>\n",
       "      <td>1.42</td>\n",
       "      <td>1.570</td>\n",
       "      <td>1.26('2018/11/28')</td>\n",
       "      <td>10.37('2007/9/10')</td>\n",
       "      <td>2.17</td>\n",
       "      <td>1.501</td>\n",
       "      <td>13.31</td>\n",
       "      <td>11.099</td>\n",
       "      <td>6.23('2008/11/4')</td>\n",
       "      <td>168.03('2016/10/25')</td>\n",
       "      <td>20.000</td>\n",
       "      <td>17.738</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000016.XSHG</th>\n",
       "      <td>上证50</td>\n",
       "      <td>2.10</td>\n",
       "      <td>1.860</td>\n",
       "      <td>1.62('2018/6/1')</td>\n",
       "      <td>5.22('2015/6/8')</td>\n",
       "      <td>2.24</td>\n",
       "      <td>0.600</td>\n",
       "      <td>17.19</td>\n",
       "      <td>18.890</td>\n",
       "      <td>15.8('2011/12/27')</td>\n",
       "      <td>394.56('2015/4/14')</td>\n",
       "      <td>25.250</td>\n",
       "      <td>62.944</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000813.XSHG</th>\n",
       "      <td>细分化工</td>\n",
       "      <td>1.83</td>\n",
       "      <td>2.168</td>\n",
       "      <td>1.8('2018-11-26')</td>\n",
       "      <td>7.59('2015-06-12')</td>\n",
       "      <td>2.92</td>\n",
       "      <td>0.898</td>\n",
       "      <td>16.85</td>\n",
       "      <td>20.326</td>\n",
       "      <td>15.82('2018-10-26')</td>\n",
       "      <td>555.27('2015-08-19')</td>\n",
       "      <td>46.770</td>\n",
       "      <td>73.654</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000814.XSHG</th>\n",
       "      <td>细分医药</td>\n",
       "      <td>4.18</td>\n",
       "      <td>4.540</td>\n",
       "      <td>4.07('2018-10-18')</td>\n",
       "      <td>10.8('2015-06-15')</td>\n",
       "      <td>5.80</td>\n",
       "      <td>1.021</td>\n",
       "      <td>25.22</td>\n",
       "      <td>31.822</td>\n",
       "      <td>24.83('2018-10-18')</td>\n",
       "      <td>158.26('2013-05-30')</td>\n",
       "      <td>51.590</td>\n",
       "      <td>21.717</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000815.XSHG</th>\n",
       "      <td>细分食品</td>\n",
       "      <td>3.73</td>\n",
       "      <td>3.500</td>\n",
       "      <td>3.27('2013-06-27')</td>\n",
       "      <td>8.2('2015-06-30')</td>\n",
       "      <td>4.89</td>\n",
       "      <td>1.232</td>\n",
       "      <td>30.20</td>\n",
       "      <td>39.716</td>\n",
       "      <td>29.57('2018-12-19')</td>\n",
       "      <td>267.98('2012-04-11')</td>\n",
       "      <td>68.790</td>\n",
       "      <td>27.265</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000005.XSHG</th>\n",
       "      <td>商业指数</td>\n",
       "      <td>3.38</td>\n",
       "      <td>3.570</td>\n",
       "      <td>2.81('2018/10/18')</td>\n",
       "      <td>46.75('2016/1/11')</td>\n",
       "      <td>7.09</td>\n",
       "      <td>7.582</td>\n",
       "      <td>53.36</td>\n",
       "      <td>43.651</td>\n",
       "      <td>36.42('2018/10/18')</td>\n",
       "      <td>1033.25('2015/4/27')</td>\n",
       "      <td>83.295</td>\n",
       "      <td>94.086</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000006.XSHG</th>\n",
       "      <td>地产指数</td>\n",
       "      <td>1.54</td>\n",
       "      <td>1.650</td>\n",
       "      <td>1.39('2012/8/21')</td>\n",
       "      <td>33.87('2014/6/5')</td>\n",
       "      <td>2.49</td>\n",
       "      <td>3.150</td>\n",
       "      <td>16.76</td>\n",
       "      <td>18.676</td>\n",
       "      <td>12.29('2018/10/19')</td>\n",
       "      <td>270.54('2015/6/12')</td>\n",
       "      <td>33.920</td>\n",
       "      <td>26.588</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000007.XSHG</th>\n",
       "      <td>公用指数</td>\n",
       "      <td>1.95</td>\n",
       "      <td>1.960</td>\n",
       "      <td>1.59('2012/12/3')</td>\n",
       "      <td>11.32('2014/12/29')</td>\n",
       "      <td>3.32</td>\n",
       "      <td>1.058</td>\n",
       "      <td>34.91</td>\n",
       "      <td>30.811</td>\n",
       "      <td>28.23('2014/1/20')</td>\n",
       "      <td>227.03('2012/5/23')</td>\n",
       "      <td>51.750</td>\n",
       "      <td>36.254</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000015.XSHG</th>\n",
       "      <td>红利指数</td>\n",
       "      <td>1.42</td>\n",
       "      <td>1.570</td>\n",
       "      <td>1.26('2018/11/28')</td>\n",
       "      <td>10.37('2007/9/10')</td>\n",
       "      <td>2.17</td>\n",
       "      <td>1.501</td>\n",
       "      <td>13.31</td>\n",
       "      <td>11.099</td>\n",
       "      <td>6.23('2008/11/4')</td>\n",
       "      <td>168.03('2016/10/25')</td>\n",
       "      <td>20.000</td>\n",
       "      <td>17.738</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000816.XSHG</th>\n",
       "      <td>细分地产</td>\n",
       "      <td>1.36</td>\n",
       "      <td>1.510</td>\n",
       "      <td>1.25('2018/10/18')</td>\n",
       "      <td>5.99('2015/6/12')</td>\n",
       "      <td>2.47</td>\n",
       "      <td>0.712</td>\n",
       "      <td>10.93</td>\n",
       "      <td>14.350</td>\n",
       "      <td>10.56('2018/10/31')</td>\n",
       "      <td>256.51('2012/5/8')</td>\n",
       "      <td>36.890</td>\n",
       "      <td>41.021</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000948.XSHG</th>\n",
       "      <td>内地地产</td>\n",
       "      <td>1.34</td>\n",
       "      <td>1.630</td>\n",
       "      <td>1.2('2018/10/30')</td>\n",
       "      <td>6.63('2009/12/10')</td>\n",
       "      <td>2.76</td>\n",
       "      <td>0.868</td>\n",
       "      <td>15.22</td>\n",
       "      <td>16.610</td>\n",
       "      <td>10.2('2018/10/30')</td>\n",
       "      <td>363.9('2012/6/18')</td>\n",
       "      <td>34.070</td>\n",
       "      <td>43.148</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000043.XSHG</th>\n",
       "      <td>超大盘</td>\n",
       "      <td>2.51</td>\n",
       "      <td>1.910</td>\n",
       "      <td>1.77('2016/6/24')</td>\n",
       "      <td>6.09('2015/6/15')</td>\n",
       "      <td>2.45</td>\n",
       "      <td>0.600</td>\n",
       "      <td>17.77</td>\n",
       "      <td>14.121</td>\n",
       "      <td>12.88('2012/8/29')</td>\n",
       "      <td>99.99('2015/6/15')</td>\n",
       "      <td>23.470</td>\n",
       "      <td>16.719</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                           名称    PB   5%分位                PB最小  \\\n",
       "code                                                             \n",
       "000001.XSHG              上证指数  1.53  1.310    1.21('2014/5/7')   \n",
       "000300.XSHG             沪深300  1.54  1.290  1.17('2014-05-07')   \n",
       "000905.XSHG             中证500  1.99  1.790  1.44('2008-11-04')   \n",
       "399001.XSHE              深证成指  2.64  1.850  1.67('2005-11-15')   \n",
       "399005.XSHE              中小板指  3.40  2.982  2.39('2008-11-04')   \n",
       "399006.XSHE              创业板指  4.73  3.054  2.51('2012-12-03')   \n",
       "399016.XSHE            深证创新指数  3.59  2.775  2.57('2019-01-03')   \n",
       "399300.XSHE             沪深300  1.54  1.290  1.17('2014-05-07')   \n",
       "399975.XSHE  中证全指证券公司指数(四级行业)  2.09  1.340   1.1('2018/10/18')   \n",
       "399368.XSHE              国证军工  2.76  2.780   2.22('2012/12/3')   \n",
       "399959.XSHE              军工指数  2.59  2.883   2.42('2012/12/3')   \n",
       "399967.XSHE              中证军工  2.96  3.303  2.89('2018/10/30')   \n",
       "399241.XSHE              地产指数   NaN  2.321   1.6('2018/10/18')   \n",
       "000015.XSHG              红利指数  1.42  1.570  1.26('2018/11/28')   \n",
       "000016.XSHG              上证50  2.10  1.860    1.62('2018/6/1')   \n",
       "000813.XSHG              细分化工  1.83  2.168   1.8('2018-11-26')   \n",
       "000814.XSHG              细分医药  4.18  4.540  4.07('2018-10-18')   \n",
       "000815.XSHG              细分食品  3.73  3.500  3.27('2013-06-27')   \n",
       "000005.XSHG              商业指数  3.38  3.570  2.81('2018/10/18')   \n",
       "000006.XSHG              地产指数  1.54  1.650   1.39('2012/8/21')   \n",
       "000007.XSHG              公用指数  1.95  1.960   1.59('2012/12/3')   \n",
       "000015.XSHG              红利指数  1.42  1.570  1.26('2018/11/28')   \n",
       "000816.XSHG              细分地产  1.36  1.510  1.25('2018/10/18')   \n",
       "000948.XSHG              内地地产  1.34  1.630   1.2('2018/10/30')   \n",
       "000043.XSHG               超大盘  2.51  1.910   1.77('2016/6/24')   \n",
       "\n",
       "                            PB最大  PB中位数   PB标准差     PE    5%分位  \\\n",
       "code                                                             \n",
       "000001.XSHG   7.14('2007/10/15')   1.90   1.097  14.82  10.420   \n",
       "000300.XSHG   7.46('2007-10-16')   1.77   1.211  13.76   9.320   \n",
       "000905.XSHG   5.89('2015-06-15')   2.78   0.902  22.90  21.400   \n",
       "399001.XSHE   8.82('2007-10-16')   2.81   1.258  23.61  12.457   \n",
       "399005.XSHE   8.17('2007-10-10')   4.18   1.164  27.57  23.061   \n",
       "399006.XSHE  15.01('2015-06-03')   4.73   1.937  41.95  32.755   \n",
       "399016.XSHE   4.79('2017-11-13')   4.13   0.590  25.89  19.595   \n",
       "399300.XSHE   7.46('2007-10-16')   1.78   1.211  13.76   9.320   \n",
       "399975.XSHE     6.11('2015/6/8')   2.43   0.990  79.42  15.498   \n",
       "399368.XSHE    11.33('2015/6/2')   4.21   1.517  69.35  70.493   \n",
       "399959.XSHE    13.06('2015/6/2')   4.15   1.925  73.90  81.645   \n",
       "399967.XSHE    13.37('2015/6/2')   5.90   1.976  52.84  59.727   \n",
       "399241.XSHE  171.13('2014/12/5')   4.25  25.461    NaN  33.898   \n",
       "000015.XSHG   10.37('2007/9/10')   2.17   1.501  13.31  11.099   \n",
       "000016.XSHG     5.22('2015/6/8')   2.24   0.600  17.19  18.890   \n",
       "000813.XSHG   7.59('2015-06-12')   2.92   0.898  16.85  20.326   \n",
       "000814.XSHG   10.8('2015-06-15')   5.80   1.021  25.22  31.822   \n",
       "000815.XSHG    8.2('2015-06-30')   4.89   1.232  30.20  39.716   \n",
       "000005.XSHG   46.75('2016/1/11')   7.09   7.582  53.36  43.651   \n",
       "000006.XSHG    33.87('2014/6/5')   2.49   3.150  16.76  18.676   \n",
       "000007.XSHG  11.32('2014/12/29')   3.32   1.058  34.91  30.811   \n",
       "000015.XSHG   10.37('2007/9/10')   2.17   1.501  13.31  11.099   \n",
       "000816.XSHG    5.99('2015/6/12')   2.47   0.712  10.93  14.350   \n",
       "000948.XSHG   6.63('2009/12/10')   2.76   0.868  15.22  16.610   \n",
       "000043.XSHG    6.09('2015/6/15')   2.45   0.600  17.77  14.121   \n",
       "\n",
       "                            PE最小                  PE最大    PE中位数    PE标准差  \n",
       "code                                                                      \n",
       "000001.XSHG    9.04('2014/4/28')   72.54('2007/10/15')   17.480   11.439  \n",
       "000300.XSHG   8.18('2014-05-19')   65.63('2007-10-31')   14.140   10.291  \n",
       "000905.XSHG  17.54('2019-01-03')  103.75('2008-01-15')   38.405   19.320  \n",
       "399001.XSHE  10.61('2005-07-11')   63.44('2007-10-16')   21.750   11.092  \n",
       "399005.XSHE  19.44('2008-10-27')   80.41('2008-01-15')   34.735   11.823  \n",
       "399006.XSHE  27.99('2012-12-03')  139.96('2015-06-03')   55.045   19.503  \n",
       "399016.XSHE  18.33('2019-01-03')   40.29('2017-11-13')   33.130    6.332  \n",
       "399300.XSHE   8.18('2014-05-19')   65.63('2007-10-31')   14.150   10.290  \n",
       "399975.XSHE    12.4('2015/9/15')     82.1('2013/9/12')   37.750   14.696  \n",
       "399368.XSHE   59.6('2018/10/29')   326.97('2018/1/10')  133.360   49.503  \n",
       "399959.XSHE  65.21('2018/10/19')   333.38('2015/6/12')  145.805   54.677  \n",
       "399967.XSHE  49.07('2018/10/19')  979.83('2015/11/26')  151.990  183.414  \n",
       "399241.XSHE   16.98('2018/9/28')  464.15('2015/11/23')   83.015   69.211  \n",
       "000015.XSHG    6.23('2008/11/4')  168.03('2016/10/25')   20.000   17.738  \n",
       "000016.XSHG   15.8('2011/12/27')   394.56('2015/4/14')   25.250   62.944  \n",
       "000813.XSHG  15.82('2018-10-26')  555.27('2015-08-19')   46.770   73.654  \n",
       "000814.XSHG  24.83('2018-10-18')  158.26('2013-05-30')   51.590   21.717  \n",
       "000815.XSHG  29.57('2018-12-19')  267.98('2012-04-11')   68.790   27.265  \n",
       "000005.XSHG  36.42('2018/10/18')  1033.25('2015/4/27')   83.295   94.086  \n",
       "000006.XSHG  12.29('2018/10/19')   270.54('2015/6/12')   33.920   26.588  \n",
       "000007.XSHG   28.23('2014/1/20')   227.03('2012/5/23')   51.750   36.254  \n",
       "000015.XSHG    6.23('2008/11/4')  168.03('2016/10/25')   20.000   17.738  \n",
       "000816.XSHG  10.56('2018/10/31')    256.51('2012/5/8')   36.890   41.021  \n",
       "000948.XSHG   10.2('2018/10/30')    363.9('2012/6/18')   34.070   43.148  \n",
       "000043.XSHG   12.88('2012/8/29')    99.99('2015/6/15')   23.470   16.719  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "#结果按照如下column排列：\n",
    "#code，名称，Pb，5%分位，PB_min，PB_max，PB_中值，PB_方差，Pe，5%分位，PE_min，PE_max，PE_均值，PE_方差\n",
    "general_result = []\n",
    "for index in analyse_index_list:\n",
    "    index_valuation = index_valuation_dir + index + '.csv'\n",
    "    #df_index_valuation = pd.read_csv(index_valuation,index_col=['date'])\n",
    "    #df_index_valuation = df_index_valuation[~df_index_valuation.reset_index().duplicated().values]\n",
    "    df_index_valuation = pd.read_csv(index_valuation)\n",
    "    df_index_valuation = df_index_valuation.drop_duplicates(\"date\")\n",
    "    df_index_valuation = df_index_valuation.set_index(\"date\")\n",
    "    \n",
    "    name = df_index_info.loc[index,['display_name']].display_name\n",
    "    pb = df_index_valuation.loc[df_index_valuation.index[len(df_index_valuation.index)-1],['PB']].PB\n",
    "    pb_quantile_5 = df_index_valuation.PB.quantile(0.05)\n",
    "    pb_min = '%r(%r)'%(df_index_valuation.PB.min(),df_index_valuation.PB.idxmin())\n",
    "    pb_max = '%r(%r)'%(df_index_valuation.PB.max(),df_index_valuation.PB.idxmax())\n",
    "    pb_median = df_index_valuation.PB.median()\n",
    "    pb_std = df_index_valuation.PB.std()\n",
    "    pe = df_index_valuation.loc[df_index_valuation.index[len(df_index_valuation.index)-1],['PE']].PE\n",
    "    pe_quantile_5 = df_index_valuation.PE.quantile(0.05)\n",
    "    pe_min = '%r(%r)'%(df_index_valuation.PE.min(),df_index_valuation.PE.idxmin())\n",
    "    pe_max = '%r(%r)'%(df_index_valuation.PE.max(),df_index_valuation.PE.idxmax())\n",
    "    pe_median = df_index_valuation.PE.median()\n",
    "    pe_std = df_index_valuation.PE.std()\n",
    "    general_result.append([name,pb,pb_quantile_5,pb_min,pb_max,pb_median,pb_std,pe,pe_quantile_5,pe_min,pe_max,pe_median,pe_std])\n",
    "df_columns=[u'名称', \"PB\", u'5%分位', u'PB最小', u'PB最大', u'PB中位数', u'PB标准差', \\\n",
    "                     \"PE\", u'5%分位', u'PE最小', u'PE最大', u'PE中位数', u'PE标准差']\n",
    "pe_df = pd.DataFrame(data=general_result,index=analyse_index_list,columns=df_columns)\n",
    "pe_df.index.name = \"code\"\n",
    "pe_df\n",
    "    \n",
    "    \n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当日轮动及海龟策略结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "all_trade_day = pd.read_csv(\"C:\\\\quanttime\\\\data\\\\basic_info\\\\all_trade_day.csv\",index_col=[\"trade_date\"])\n",
    "result = []\n",
    "for index1 in df_index_info.index:\n",
    "    row_record = []\n",
    "    index_file = index_k_dir + str(index1) + \".csv\"\n",
    "    index_name = df_index_info.loc[index1,[\"display_name\"]].display_name\n",
    "    #print(index_name)\n",
    "    row_record.append(index_name)\n",
    "    try:\n",
    "        index_k_data = pd.read_csv(index_file,index_col=[\"date\"])\n",
    "        current_date = index_k_data.index[len(index_k_data.index) - 1]\n",
    "        row_record.append(current_date)\n",
    "        #1、与前20天的情况比较，当前持有则低于前20日收盘价则卖出，当前空仓则高于前20日收盘价则买入\n",
    "        back_trace = 20\n",
    "        if all_trade_day.index.tolist().index(current_date) - back_trace >= 0:\n",
    "            pre_date = all_trade_day.index[all_trade_day.index.tolist().index(current_date) - back_trace]\n",
    "            pre_value = index_k_data.loc[pre_date,['close']].close\n",
    "            current = index_k_data.loc[current_date,['close']].close\n",
    "            if float(pre_value) > float(current):\n",
    "                #compare = u'当前close低于20日前close'\n",
    "                compare = 'sell'\n",
    "                row_record.append(compare)\n",
    "                #print(\"%r-%r 当前价格低于20日前\"%(index1,index_name))\n",
    "            else:\n",
    "                #compare = u'当前close高于（或等于）20日前close'\n",
    "                compare = 'buy'\n",
    "                row_record.append(compare)\n",
    "                #print(\"%r-%r 当前价格高于20日前\"%(index1,index_name))\n",
    "        #2、前20日收盘价，前10日收盘价情况比较，当前持有则低于前10日收盘价则卖出，当前空仓则高于前20日收盘价则买入\n",
    "            back_trace = 10\n",
    "            pre_date = all_trade_day.index[all_trade_day.index.tolist().index(current_date) - back_trace]\n",
    "            if float(current) >= float(pre_value):\n",
    "                #compare = u'当前close高于等于20日前close'\n",
    "                compare = 'buy'\n",
    "                row_record.append(compare)\n",
    "            elif float(current) < float(index_k_data.loc[pre_date,['close']].close) :\n",
    "                #compare = u'当前close低于10日前close'\n",
    "                compare = 'sell'\n",
    "                row_record.append(compare)\n",
    "            else:\n",
    "                #compare = u'当前close介于20日前close与10日前close之间'\n",
    "                compare = 'hold'\n",
    "                row_record.append(compare)\n",
    "        #3、买入与前76日收盘价比较，卖出与33日收盘价比较，即当前close高于前76日close则买入或继续持有，当前close低于前33日close则卖出\n",
    "        back_trace = 76\n",
    "        if all_trade_day.index.tolist().index(current_date) - back_trace >= 0:\n",
    "            pre_date = all_trade_day.index[all_trade_day.index.tolist().index(current_date) - back_trace]\n",
    "            pre_date_33 = all_trade_day.index[all_trade_day.index.tolist().index(current_date) - 33]\n",
    "            pre_value = index_k_data.loc[pre_date,['close']].close\n",
    "            current = index_k_data.loc[current_date,['close']].close\n",
    "            #print(pre_value)\n",
    "            #print(current)\n",
    "            if float(current) >= float(pre_value):\n",
    "                #compare = u'当前close高于等于76日前close'\n",
    "                compare = 'buy'\n",
    "                row_record.append(compare)\n",
    "            elif float(current) < float(index_k_data.loc[pre_date_33,['close']].close) :\n",
    "                #compare = u'当前close低于33日前close'\n",
    "                compare = 'sell'\n",
    "                row_record.append(compare)\n",
    "            else:\n",
    "                #compare = u'当前close介于76日前close与33日前close之间'\n",
    "                compare = 'hold'\n",
    "                row_record.append(compare)\n",
    "    except:\n",
    "        print(\"%r process error\"%index1)\n",
    "        continue\n",
    "    #print(row_record)\n",
    "    result.append(row_record)\n",
    "df_columns = ['index_name','date','20buy VS 20sell',u'20buy VS 10sell',u'76buy vs 33sell']\n",
    "df = pd.DataFrame(data=result,index=df_index_info.index,columns=df_columns)\n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                   index_name        date 20buy VS 20sell 20buy VS 10sell  \\\n",
      "code                                                                        \n",
      "000001.XSHG              上证指数  2019-03-06             buy             buy   \n",
      "000002.XSHG              A股指数  2019-03-06             buy             buy   \n",
      "000003.XSHG              B股指数  2019-03-06             buy             buy   \n",
      "000004.XSHG              工业指数  2019-03-06             buy             buy   \n",
      "000005.XSHG              商业指数  2019-03-06             buy             buy   \n",
      "000006.XSHG              地产指数  2019-03-06             buy             buy   \n",
      "000007.XSHG              公用指数  2019-03-06             buy             buy   \n",
      "000008.XSHG              综合指数  2019-03-06             buy             buy   \n",
      "000009.XSHG             上证380  2019-03-06             buy             buy   \n",
      "000010.XSHG             上证180  2019-03-06             buy             buy   \n",
      "000011.XSHG              基金指数  2019-03-06             buy             buy   \n",
      "000012.XSHG              国债指数  2019-03-06             buy             buy   \n",
      "000013.XSHG           上证企业债指数  2019-03-06             buy             buy   \n",
      "000015.XSHG              红利指数  2019-03-06             buy             buy   \n",
      "000016.XSHG              上证50  2019-03-06             buy             buy   \n",
      "000017.XSHG               新综指  2019-03-06             buy             buy   \n",
      "000018.XSHG             180金融  2019-03-06             buy             buy   \n",
      "000019.XSHG              治理指数  2019-03-06             buy             buy   \n",
      "000020.XSHG              中型综指  2019-03-06             buy             buy   \n",
      "000021.XSHG             180治理  2019-03-06             buy             buy   \n",
      "000022.XSHG           上证公司债指数  2019-03-06             buy             buy   \n",
      "000025.XSHG             180基建  2019-03-06             buy             buy   \n",
      "000026.XSHG             180资源  2019-03-06             buy             buy   \n",
      "000027.XSHG             180运输  2019-03-06             buy             buy   \n",
      "000028.XSHG             180成长  2019-03-06             buy             buy   \n",
      "000029.XSHG             180价值  2019-03-06             buy             buy   \n",
      "000030.XSHG            180R成长  2019-03-06             buy             buy   \n",
      "000031.XSHG            180R价值  2019-03-06             buy             buy   \n",
      "000032.XSHG              上证能源  2019-03-06             buy             buy   \n",
      "000033.XSHG              上证材料  2019-03-06             buy             buy   \n",
      "...                       ...         ...             ...             ...   \n",
      "399968.XSHE       沪深300周期行业指数  2019-03-06             buy             buy   \n",
      "399969.XSHE      沪深300非周期行业指数  2019-03-06             buy             buy   \n",
      "399970.XSHE         中证移动互联网指数  2019-03-06             buy             buy   \n",
      "399971.XSHE            中证传媒指数  2019-03-06             buy             buy   \n",
      "399972.XSHE             300深市  2019-03-06             buy             buy   \n",
      "399973.XSHE            中证国防指数  2019-03-06             buy             buy   \n",
      "399974.XSHE        中证国有企业改革指数  2019-03-06             buy             buy   \n",
      "399975.XSHE  中证全指证券公司指数(四级行业)  2019-03-06             buy             buy   \n",
      "399976.XSHE         中证新能源汽车指数  2019-03-06             buy             buy   \n",
      "399977.XSHE      中证内地低碳经济主题指数  2019-03-06             buy             buy   \n",
      "399978.XSHE         中证医药100指数  2019-03-06             buy             buy   \n",
      "399979.XSHE        中证大宗商品股票指数  2019-03-06             buy             buy   \n",
      "399980.XSHE          中证超级大盘指数  2019-03-06             buy             buy   \n",
      "399981.XSHE    沪深300行业分层等权重指数  2019-03-06             buy             buy   \n",
      "399982.XSHE        中证500等权重指数  2019-03-06             buy             buy   \n",
      "399983.XSHE      沪深300地产等权重指数  2019-03-06             buy             buy   \n",
      "399984.XSHE        沪深300等权重指数  2019-03-06             buy            hold   \n",
      "399985.XSHE            中证全指指数  2019-03-06             buy             buy   \n",
      "399986.XSHE            中证银行指数  2019-03-06             buy             buy   \n",
      "399987.XSHE             中证酒指数  2019-03-06             buy             buy   \n",
      "399989.XSHE            中证医疗指数  2019-03-06             buy             buy   \n",
      "399990.XSHE          中证煤炭等权指数  2019-03-06             buy             buy   \n",
      "399991.XSHE        中证一带一路主题指数  2019-03-06             buy             buy   \n",
      "399992.XSHE        中证万得并购重组指数  2019-03-06             buy             buy   \n",
      "399993.XSHE        中证万得生物科技指数  2019-03-06             buy             buy   \n",
      "399994.XSHE        中证信息安全主题指数  2019-03-06             buy             buy   \n",
      "399995.XSHE          中证基建工程指数  2019-03-06             buy             buy   \n",
      "399996.XSHE          中证智能家居指数  2019-03-06             buy             buy   \n",
      "399997.XSHE            中证白酒指数  2019-03-06             buy             buy   \n",
      "399998.XSHE            中证煤炭指数  2019-03-06             buy             buy   \n",
      "\n",
      "            76buy vs 33sell  \n",
      "code                         \n",
      "000001.XSHG             buy  \n",
      "000002.XSHG             buy  \n",
      "000003.XSHG             buy  \n",
      "000004.XSHG             buy  \n",
      "000005.XSHG             buy  \n",
      "000006.XSHG             buy  \n",
      "000007.XSHG             buy  \n",
      "000008.XSHG             buy  \n",
      "000009.XSHG             buy  \n",
      "000010.XSHG             buy  \n",
      "000011.XSHG             buy  \n",
      "000012.XSHG             buy  \n",
      "000013.XSHG             buy  \n",
      "000015.XSHG             buy  \n",
      "000016.XSHG             buy  \n",
      "000017.XSHG             buy  \n",
      "000018.XSHG             buy  \n",
      "000019.XSHG             buy  \n",
      "000020.XSHG             buy  \n",
      "000021.XSHG             buy  \n",
      "000022.XSHG             buy  \n",
      "000025.XSHG             buy  \n",
      "000026.XSHG             buy  \n",
      "000027.XSHG             buy  \n",
      "000028.XSHG             buy  \n",
      "000029.XSHG             buy  \n",
      "000030.XSHG             buy  \n",
      "000031.XSHG             buy  \n",
      "000032.XSHG             buy  \n",
      "000033.XSHG             buy  \n",
      "...                     ...  \n",
      "399968.XSHE             buy  \n",
      "399969.XSHE             buy  \n",
      "399970.XSHE             buy  \n",
      "399971.XSHE             buy  \n",
      "399972.XSHE             buy  \n",
      "399973.XSHE             buy  \n",
      "399974.XSHE             buy  \n",
      "399975.XSHE             buy  \n",
      "399976.XSHE             buy  \n",
      "399977.XSHE             buy  \n",
      "399978.XSHE             buy  \n",
      "399979.XSHE             buy  \n",
      "399980.XSHE             buy  \n",
      "399981.XSHE             buy  \n",
      "399982.XSHE             buy  \n",
      "399983.XSHE             buy  \n",
      "399984.XSHE            hold  \n",
      "399985.XSHE             buy  \n",
      "399986.XSHE             buy  \n",
      "399987.XSHE             buy  \n",
      "399989.XSHE             buy  \n",
      "399990.XSHE             buy  \n",
      "399991.XSHE             buy  \n",
      "399992.XSHE             buy  \n",
      "399993.XSHE             buy  \n",
      "399994.XSHE             buy  \n",
      "399995.XSHE             buy  \n",
      "399996.XSHE             buy  \n",
      "399997.XSHE             buy  \n",
      "399998.XSHE             buy  \n",
      "\n",
      "[675 rows x 5 columns]\n"
     ]
    }
   ],
   "source": [
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2018, 12, 27, 11, 41, 36, 891978)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t1 = datetime.now()\n",
    "t1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
